package com.xdja.netty.conf;

import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * description
 *
 * @author zk
 * @since 2018/7/23
 */
public class HttpManager {
    private static Logger logger = LoggerFactory.getLogger(HttpManager.class);
    /**
     * httpUrlMapping<url,Entry></>
     */
    private static Map<String, Entry<HttpRequest, HttpContent>> httpRequestMapping = new HashMap<>();

    /**
     * 存放mapping
     *
     * @param url url
     * @param httpEntry http实体
     */
    public static void addMapping(String url,Entry<HttpRequest, HttpContent> httpEntry){
        if(url == null || httpEntry == null){
            logger.warn("add mapping has null");
            return;
        }
        httpRequestMapping.put(url,httpEntry);
    }

    /**
     * 获取mapping
     *
     * @param url url
     * @return httpEntry
     */
    public static Entry<HttpRequest, HttpContent> getMapping(String url){
        return httpRequestMapping.get(url);
    }
    /**
     * http组合
     *
     * @param <M> httpRequest
     * @param <N> httpContent
     */
    public static class Entry<M extends HttpRequest, N extends HttpContent> {
        private M httpRequest;
        private N httpContent;

        public M getHttpRequest() {
            return httpRequest;
        }

        public void setHttpRequest(M httpRequest) {
            this.httpRequest = httpRequest;
        }

        public N getHttpContent() {
            return httpContent;
        }

        public void setHttpContent(N httpContent) {
            this.httpContent = httpContent;
        }

        /**
         * 清空该组合
         */
        public void clearMe() {
            this.httpRequest = null;
            this.httpContent = null;
        }

        /**
         * 复制该组合
         *
         * @return copy file of this
         */
        public Entry<M, N> copyMe() {
            Entry<M, N> copyEntity = new Entry<>();
            copyEntity.setHttpRequest(this.httpRequest);
            copyEntity.setHttpContent(this.httpContent);
            return copyEntity;
        }
    }
}
